package com.github.ilioili.justdoit.components.fragment;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.github.ilioili.justdoit.R;
import com.github.ilioili.justdoit.model.pojo.ScheduleEvent;
import com.taihe.template.base.BaseFragment;
import com.taihe.template.base.injection.Id;
import com.taihe.template.base.injection.Layout;

/**
 * Created by hantuo on 16/5/18.
 */
@Layout(R.layout.fragment_to_do)
public class RoutineFragment extends BaseFragment {
    @Id(R.id.viewPager)
    private ViewPager viewPager;
    private ToDoListOfTodayFragment toDoListOfTodayFragment;
    private HabitsTabFragment scrollableTabFragment;

    @Override
    protected void init(View rootView) {
        super.init(rootView);
        toDoListOfTodayFragment = ToDoListOfTodayFragment.newInstance();
        scrollableTabFragment = HabitsTabFragment.newInstance();
        viewPager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                if (position == 0) {
                    return toDoListOfTodayFragment;
                } else {
                    return scrollableTabFragment;
                }
            }

            @Override
            public int getCount() {
                return 2;
            }
        });
    }

    public boolean toggleFragment() {
        if (viewPager.getCurrentItem() == 0) {
            viewPager.setCurrentItem(1, true);
            return false;
        } else {
            viewPager.setCurrentItem(0, true);
            return true;
        }
    }

    public static RoutineFragment newInstance() {
        return new RoutineFragment();
    }

    public void addHabbit(ScheduleEvent scheduleEvent) {
        scrollableTabFragment.addHabbit(scheduleEvent);
        toDoListOfTodayFragment.addHabbit(scheduleEvent);
    }
}
